#!/bin/bash

set -ex
#pushd /sgl-workspace/sglang/python
export RES_DIR_PATH=/sgl-workspace/sglang/python

model_dir=/v1/models
port=30011
max_concurrency=10,64,128
num_prompts=8
performance_test_scale=32,64,128
while getopts "m:p:" opt; do
    case $opt in
        m) model_dir=$OPTARG ;;
        p) port=$OPTARG ;;
        *) echo "Usage: $0 [-m hf_model_dir] [-p port]"
           exit 1
           ;;
    esac
done


IFS=',' read -ra substrings <<< "$performance_test_scale"
echo 'max_concurrency,random_input_len,random_output_len,total_input_tokens,total_output_tokens,concurrency,duration,request_throughput,input_throughput,output_throughput,total_throughput,mean_ttft_ms,median_ttft_ms,std_ttft_ms,p99_ttft_ms,mean_tpot_ms,median_tpot_ms,std_tpot_ms,p99_tpot_ms,mean_itl_ms,median_itl_ms,std_itl_ms,p99_itl_ms' > $1

for max_concurrency in {74,100,150}; do
    # num_prompts=$((2 * $max_concurrency))
    # for ((put_len=32; put_len<=8192; put_len*=2)); do
    for ((i=0; i<3; i++)); do
        python3 -m sglang.bench_serving \
        --host 0.0.0.0 \
        --port $port \
        --backend sglang \
        --model $model_dir \
        --dataset-path /models/dataset/ShareGPT_V3_unfiltered_cleaned_split.json \
        --max-concurrency $max_concurrency \
        --output-file subres$max_concurrency-$put_len-$i.json \
        --num-prompts $max_concurrency \
        --random-input-len $put_len \
        --random-output-len $put_len 
    done
    cat $RES_DIR_PATH/subres$max_concurrency-$put_len-2.json|jq -cr '.max_concurrency,.random_input_len,.random_output_len,.total_input_tokens,.total_output_tokens,.concurrency,.duration,.request_throughput,.input_throughput,.output_throughput,(.input_throughput + .output_throughput),.mean_ttft_ms,.median_ttft_ms,.std_ttft_ms,.p99_ttft_ms,.mean_tpot_ms,.median_tpot_ms,.std_tpot_ms,.p99_tpot_ms,.mean_itl_ms,.median_itl_ms,.std_itl_ms,.p99_itl_ms'|tr '\n' ',' |sed 's/,$/\n/' >> $1
    rm $RES_DIR_PATH/subres$max_concurrency-$put_len-0.json
    rm $RES_DIR_PATH/subres$max_concurrency-$put_len-1.json
    rm $RES_DIR_PATH/subres$max_concurrency-$put_len-2.json
    # done
done
#popd
